package com.puyuba.puyuba.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.puyuba.puyuba.R;
import com.puyuba.puyuba.bean.NeedBean;
import com.puyuba.puyuba.bean.TextselectBean;
import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.network.RequestBodyUtil;
import com.pyb.java.network.ResponseEntity;
import com.pyb.java.network.RetrofitManger;
import com.pyb.java.network.observers.ResponseObserve;

import com.pyb.java.network.scheduler.RxSchedulers;
import com.puyuba.puyuba.network.UserApi;
import com.pyb.java.commonutil.utils.StringUtil;
import com.pyb.java.commonutil.ToastHelper;

import com.puyuba.puyuba.view.adapter.ChooseAdatper;

import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

/**
 * 职业成长需求dialog
 */

public class ProfessionDemandDialog extends Dialog {

    private EditText etAge;
    private EditText etjob;
    private EditText etCompany;
    private RecyclerView rclProfession;
    private RelativeLayout rlBg;
    private TextView tvCommit;
    private ImageView ivClose;
    private TextView tvTime1;
    private TextView tvTime2;
    private TextView tvTime3;
    private TextView tvTime4;
    String list2 = "[{\"name\":\"教师资格\"},{\"name\":\"建筑工程\"},{\"name\":\"医护药师\"},{\"name\":\"金融考试\"},{\"name\":\"人力资源\"},{\"name\":\"英语考级\"}," +
            "{\"name\":\"营养健康\"},{\"name\":\"经济财会\"},{\"name\":\"其他\"},]";
    private int choose = 4;
    private List<TextselectBean> professionlist;
    private int formposition;
    private String age; //年龄
    private String job; //职业
    private String company; //企业
    private String profession = ""; //需求
    private String year = ""; //时间
    private ChooseAdatper chooseAdatper;

    public ProfessionDemandDialog(Context context) {
        super(context, R.style.ContactDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_profession_demand_layout);
        initView();
        //初始化界面控件的事件
        initEvent();
        getneed(1, CacheUserInfo.getInstance().getHxUid());
    }

    //查询学生提交需求
    public void getneed(int type,String uid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("uid", uid);
        RequestBody bodyByMap = RequestBodyUtil.getRequestBodyByMap(map);
        RetrofitManger.getInstance().createService(UserApi.class)
                .getneed(bodyByMap)
                .compose(RxSchedulers.<ResponseEntity<NeedBean>>applySchedulers())
                .subscribe(new ResponseObserve<NeedBean>(){

                    @Override
                    public void onSuccess(ResponseEntity<NeedBean> responseEntity) {
                        setData(responseEntity.getData());
                    }

                    @Override
                    public void onFail(String errorMsg) {
                        if (errorMsg != null&&!errorMsg.equals("")){
                            ToastHelper.show(errorMsg);
                        }else {
                            ToastHelper.show("服务器繁忙，请稍后再试");
                        }
                    }

                    @Override
                    public void onSubscribeStart(@NonNull Disposable disposable) { }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }

                });
    }
    public void setData(NeedBean data) {
        etAge.setText(data.getAge()+"");
        etjob.setText(data.getJob());
        etCompany.setText(data.getCompany());
        if (data.getYear().equals("0.5年")){
            tvTime1.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvTime1.setTextColor(getContext().getColor(R.color.color_181C2F));
            year = tvTime1.getText().toString();
            choose = 0;
        }else if (data.getYear().equals("1年")){
            tvTime2.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvTime2.setTextColor(getContext().getColor(R.color.color_181C2F));
            year = tvTime2.getText().toString();
            choose = 1;
        }else if (data.getYear().equals("1.5年")){
            tvTime3.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvTime3.setTextColor(getContext().getColor(R.color.color_181C2F));
            year = tvTime3.getText().toString();
            choose = 2;
        }else if (data.getYear().equals("2年")){
            tvTime4.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvTime4.setTextColor(getContext().getColor(R.color.color_181C2F));
            year = tvTime4.getText().toString();
            choose = 3;
        }

        profession = data.getIndustry();
        for (int a = 0; a < professionlist.size(); a++){
            if (professionlist.get(a).getName().equals(profession)){
                professionlist.get(a).setSelect(true);
            }else {
                professionlist.get(a).setSelect(false);
            }
        }
        chooseAdatper.notifyDataSetChanged();
    }
    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        TextView[] textViews = {tvTime1,tvTime2,tvTime3,tvTime4};
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        rlBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                age = etAge.getText().toString().trim();
                job = etjob.getText().toString().trim();
                company = etCompany.getText().toString().trim();
                if ( onClickBottomListener!= null) {
                    if (!StringUtil.isEmpty(age)&&!StringUtil.isEmpty(job)&&!StringUtil.isEmpty(company)
                            &&!StringUtil.isEmpty(profession)&&!StringUtil.isEmpty(year)){
                        dismiss();
                        onClickBottomListener.commit(age,job,company,profession,year);
                    }else {
                        ToastHelper.show("请填写需求");
                    }
                }
            }
        });
        tvTime1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTime1.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvTime1.setTextColor(getContext().getColor(R.color.color_181C2F));
                year = tvTime1.getText().toString();
                if (choose<4){
                    textViews[choose].setBackground(null);
//                    textViews[choose].setTextColor(getContext().getColor(R.color.color_4e4e4e));
                }
                choose = 0;
            }
        });
        tvTime2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTime2.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvTime2.setTextColor(getContext().getColor(R.color.color_181C2F));
                year = tvTime2.getText().toString();
                if (choose<4){
                    textViews[choose].setBackground(null);
//                    textViews[choose].setTextColor(getContext().getColor(R.color.color_4e4e4e));
                }
                choose = 1;
            }
        });
        tvTime3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTime3.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvTime3.setTextColor(getContext().getColor(R.color.color_181C2F));
                year = tvTime3.getText().toString();
                if (choose<4){
                    textViews[choose].setBackground(null);
//                    textViews[choose].setTextColor(getContext().getColor(R.color.color_4e4e4e));
                }
                choose = 2;
            }
        });
        tvTime4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTime4.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvTime4.setTextColor(getContext().getColor(R.color.color_181C2F));
                year = tvTime4.getText().toString();
                if (choose<4){
                    textViews[choose].setBackground(null);
//                    textViews[choose].setTextColor(getContext().getColor(R.color.color_4e4e4e));
                }
                choose = 3;
            }
        });

        professionlist = JSONObject.parseArray(list2, TextselectBean.class);

        chooseAdatper = new ChooseAdatper(getContext(), professionlist);
        GridLayoutManager manager = new GridLayoutManager(getContext(),3);
        manager.setOrientation(GridLayoutManager.VERTICAL);

        rclProfession.setLayoutManager(manager);
        rclProfession.setAdapter(chooseAdatper);
        chooseAdatper.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                hideSoftKeyboard();
                profession = professionlist.get(position).getName();
                formposition = position;
                for (int a = 0; a < professionlist.size(); a++){
                    if (a == formposition){
                        professionlist.get(a).setSelect(true);
                    }else {
                        professionlist.get(a).setSelect(false);
                    }
                }
                chooseAdatper.notifyDataSetChanged();
            }
        });
    }

    /**
     * 隐藏软键盘(只适用于Activity，不适用于Fragment)
     */
    public void hideSoftKeyboard() {
        //解决华为机型安全键盘遮挡toast
        View focusView = getWindow().getDecorView().findFocus();
        if (Build.MANUFACTURER.equalsIgnoreCase("huawei")){
            if (focusView instanceof EditText){
                EditText et = (EditText) focusView;
                if(et.getInputType()==(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT)){
                    InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context
                            .INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(focusView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        }
    }
    /**
     * 设置确定取消按钮的回调
     */
    public OnClickBottomListener onClickBottomListener;
    public ProfessionDemandDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
        return this;
    }



    public interface OnClickBottomListener{

        public void commit(String age, String job, String company, String profession, String year);
    }
    private void initView() {
        rlBg = (RelativeLayout) findViewById(R.id.rl_bg);
        tvCommit = (TextView) findViewById(R.id.tv_commit);
        ivClose = (ImageView) findViewById(R.id.iv_close);
        etAge = (EditText) findViewById(R.id.et_age);
        etjob = (EditText) findViewById(R.id.et_job);
        etCompany = (EditText) findViewById(R.id.et_company);
        rclProfession = (RecyclerView) findViewById(R.id.rcl_profession);
        tvTime1 = (TextView) findViewById(R.id.tv_time1);
        tvTime2 = (TextView) findViewById(R.id.tv_time2);
        tvTime3 = (TextView) findViewById(R.id.tv_time3);
        tvTime4 = (TextView) findViewById(R.id.tv_time4);

    }


}